<?php


namespace app\index\controller;


use app\common\controller\Basecontroller;
use app\common\controller\Map;
use app\common\controller\OssStorage;
use app\index\model\Coenvironment;
use app\index\model\Comassage;
use app\index\model\Coproduct;
use app\index\model\Cotype;
use app\index\model\Img;
use app\index\model\User;
use think\Db;
use think\Request;
use app\index\model\Company;
use app\validate\Vali;
use think\facade\Cache;

/**
 * ljj
 * registerCompany 注册公司接口
 * 未完成
 */
class RegisterCompany extends Basecontroller
{
    public function randStr()
    {
        $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $BossPassword = '';
        for ($i = 0; $i < 8; $i++) {
            $num = mt_rand(0, strlen($str) - 1);
            $BossPassword .= $str[$num];
        }
        return $BossPassword;
    }
    public function companyRegister(Map $map,Vali $vali, Request $request, Img $img, Comassage $comassage, Coproduct $coproduct, Cotype $cotype, Coenvironment $coenvironment, User $user, Company $company)
    {
        $userId = $this->getUserID();
        $checkCompany = $company->isRegisterCompany($userId);
        if (!empty($checkCompany)) return $this->returnJson(self::ERROR,'该用户已有公司');
        $data = $request->post();
        $file = $request->file();
        $logoUrl = $file['company_logo'];$EnvironImg = $file['environImg'];
        $ProductImg = $file['productImg'];
        $randStr = $this->randStr();
        $productInf = [];
        $productSrc = [];
        $companyInf = [
            'company_introduction' => $data['company_introduction'],
            'company_name' => $data['company_name'],
            'company_area' => $data['company_address'],
            'company_welfare' => $data['company_welfare'],
            'range_money' => $data['range_money'],
            'com_official' => $data['com_official']
        ];
        $companyType = [
            'type' => $data['company_type']
        ];
        $companyMassage = [
            'add_money' => $data['add_money'],
            'management' => $data['management'],
            'company_addtime' => $data['company_addtime']
        ];
        if (!$vali->fileExt($logoUrl,['ext'=>'jpeg','jpg','png','gif','size'=>1500000])) return $this->returnJson(self::OK, '请上传小于1.5M且格式为jpg,png,jpeg,gif');
        foreach ($EnvironImg as $key => $value) {
            if ($vali->fileExt($EnvironImg,['ext'=>'png,jpg,jpeg','size'=>1500000])) return $this->returnJson(self::OK, '请上传小于1.5M且格式为jpg,png,jpeg,gif');
        }
        if (!$vali->scene('registerCompany')->batch()->check($companyInf)) return $this->returnJson(self::ERROR, '注册格式不正确', $vali->getError());
        if (!$vali->scene('companyType')->batch()->check($companyType)) return $this->returnJson(self::ERROR, '注册格式不正确', $vali->getError());
        if (!$vali->scene('companyMassage')->batch()->check($companyMassage)) return $this->returnJson(self::ERROR, '注册格式不正确', $vali->getError());
        foreach ($data['product'] as $key => $value) {
            $companyProduct = [
                'coproduct' => $value,
                'product_introduction' => $data['product_introduction'][$key],
                'producType' => $data['productType'][$key]
            ];
            if (!$vali->scene('Product')->batch()->check($companyProduct)) return $this->returnJson(self::ERROR, '注册格式不正确', $vali->getError());
        }
        foreach ($ProductImg as $key => $value) {
            if ($vali->fileExt($value,['ext'=>'png,jpg,jpeg','size'=>1500000])) return $this->returnJson(self::OK, '请上传小于1.5M且格式为jpg,png,jpeg,gif');
        }
        $Environ = [];
        $invCode = rand(100000,999999);
        $logo = OssStorage::fileHandle($logoUrl);
        $logoSrc = OssStorage::upload($logo['dst'], $logo['src']);
        $companyInf = [
            'company_logo' => $logoSrc,
            'company_introduction' => $data['company_introduction'],
            'company_name' => $data['company_name'],
            'company_area' => $data['company_address'],
            'company_welfare' => $data['company_welfare'],
            'range_money' => $data['range_money'],
            'com_official' => $data['com_official'],
            'invCode' => $invCode
        ];
        $company->insertCompany($companyInf);
        $companyId = Db::table('company')->order('company_id', 'desc')->find();
        foreach ($EnvironImg as $key => $value) {
            $environImg = OssStorage::fileHandle($value);
            $Environ[$key] = OssStorage::upload($environImg['dst'], $environImg['src']);
        }
        foreach ($ProductImg as $key => $value) {
            $product = OssStorage::fileHandle($value);
            $productSrc[$key] = OssStorage::upload($product['dst'], $product['src']);
            $insertProduct = [
                'company_id' => $companyId['company_id'],
                'coproduct' => $data['product'][$key],
                'product_img' => $productSrc[$key],
                'productType' => $data['productType'][$key],
                'product_introduction' => $data['product_introduction'][$key]
            ];
            $productInf[$key] = $insertProduct;
        }
        $companyType = [
            'company_id' => $companyId['company_id'],
            'type' => $data['company_type']
        ];
        $environ = [
            ['company_id'=>$companyId['company_id'],'coenviron_img'=>$Environ[0]],
            ['company_id'=>$companyId['company_id'],'coenviron_img'=>$Environ[1]],
            ['company_id'=>$companyId['company_id'],'coenviron_img'=>$Environ[2]],
            ['company_id'=>$companyId['company_id'],'coenviron_img'=>$Environ[3]]
            ];
        $passNum = rand(1000,9999);
        $phone = rand(83000000000, 89999999999);
        $boss = [
            'username' => $data['boss_name'],
            'phone' => $phone,
            'password' => md5(md5($passNum).md5($randStr)),
            'company_id' => $companyId['company_id'],
            'position' => '2',
            'salt' => md5($passNum),
            'create_time' => time(),
            'update_time' => time()
        ];
        $user->insertBoss($boss);
        $cotype->insertCompanyType($companyType);
        $bossId = Db::table('user')->order('user_id', 'desc')->find();
        $cotypeId = Db::table('cotype')->order('cotype_id', 'desc')->find();
        $companyMassage = [
            'company_id' => $companyId['company_id'],
            'add_money' => $data['add_money'],
            'cotype_id' => $cotypeId['cotype_id'],
            'company_addtime' => $data['company_addtime'],
            'management' => $data['management'],
            'user_id' => $bossId['user_id']
        ];
        $coproduct->insertCoproduct($productInf);
        $coenvironment->insertEnviron($environ);
        $comassage->insertMassage($companyMassage);
        $user->getUser($userId,$companyId['company_id']);
        return $this->returnJson(self::OK,'注册成功',['username'=>$bossId['phone'],'password'=>$randStr,'InvCode' => $invCode]);
    }
}